package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.repository.StrategyRepository;
import cn.wolfcode.wolf2w.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public  class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private MongoTemplate template;
    @Autowired
    private StrategyRepository repository ;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService ;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private StrategyCatalogRepository CatalogRepository;
    @Autowired
    private IStrategyCommentService strategyCommentService;


    @Override
    public void save(Strategy strategy) {
        strategy.setId(null);
        repository.save(strategy);
    }

    @Override
    public void update(Strategy strategy) {
        repository.save(strategy);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Strategy get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Strategy> list() {
        return repository.findAll();
    }

    @Override
    public Page<Strategy> query(StrategyQuery qo) {
        Query query = new Query();

        if(StringUtils.hasLength(qo.getThemeId())){
            query.addCriteria(Criteria.where("themeId").is(qo.getThemeId()));
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        long count = template.count(query, Strategy.class);
        if(count==0){
            Page.empty();
        }
        if(count%qo.getPageSize()==0&&qo.getCurrentPage()>1){
            qo.setCurrentPage(qo.getCurrentPage()-1);
        }
        query.with(pageable);
        List<Strategy> strategies = template.find(query, Strategy.class);
        return new PageImpl<>(strategies, pageable,count );
    }

    @Override
    public void saveOrUpdate(Strategy strategy) {
        strategy.setThemeName(strategyThemeService.get(strategy.getThemeId()).getName());
        strategy.setCatalogName(strategyCatalogService.get(strategy.getCatalogId()).getName());
        strategy.setDestId(strategyCatalogService.get(strategy.getCatalogId()).getDestId());
        strategy.setDestName(strategyCatalogService.get(strategy.getCatalogId()).getDestName());

        List<Destination> toasts = destinationService.getToasts(strategy.getDestId());
        if(toasts.contains("中国")){
            strategy.setIsabroad(Strategy.ABROAD_NO);
        }else {
            strategy.setIsabroad(Strategy.ABROAD_YES);
        }

        if(StringUtils.hasLength(strategy.getId())){
            Strategy strategy1 = this.get(strategy.getId());
            strategy.setCreateTime(strategy1.getCreateTime());
            strategy.setViewnum(strategy1.getViewnum());
            strategy.setFavornum(strategy1.getFavornum());
            strategy.setReplynum(strategy1.getReplynum());
            strategy.setSharenum(strategy1.getSharenum());
            strategy.setThumbsupnum(strategy1.getThumbsupnum());
            this.update(strategy);
        }else {
            strategy.setCreateTime(new Date());
            this.save(strategy);
        }
    }

    @Override
    public void changeState(String id, int state) {
        Strategy strategy = this.get(id);
        if(state==Strategy.STATE_NORMAL){
            strategy.setState(state);

        }else {
            strategy.setState(Strategy.STATE_PUBLISH);
        }
        this.update(strategy);
    }



    @Override
    public List<Strategy> queryViewnumTop3(String destId) {
        Pageable pageable = PageRequest.of(0,3 , Sort.Direction.DESC,"viewnum");
        List<Strategy> strategies = repository.findByDestId(destId ,pageable);

        return strategies;
    }

    @Override
    public void updateStatisVo(StrategyStatisVO vo) {
          Query query = new Query() ;
          query.addCriteria(Criteria.where("_id").is(vo.getStrategyId()));
        Update update = new Update();
        update.set("viewnum",vo.getViewnum());
        update.set("favornum",vo.getFavornum());
        update.set("replynum",vo.getReplynum());
        update.set("thumbsupnum",vo.getThumbsupnum());
        update.set("sharenum",vo.getSharenum());
        template.updateMulti(query,update ,Strategy.class );
    }

    @Override
    public List<Strategy> findByDestName(String keyword) {
        return repository.findByDestName(keyword);
    }
}
